package com.metaera.app.ui.core;

import android.os.Bundle;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.metaera.app.R;
import com.metaera.app.controller.core.IPaginationPresenter;
import com.metaera.app.controller.view.BasicPaginationView;
import com.metaera.app.controller.view.RefreshLoadMoreView;
import com.metaera.app.controller.view.core.IPaginationView;
import com.metaera.app.utils.ActivityTitleHelper;
import com.metaera.app.utils.ViewUtils;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;

import androidx.recyclerview.widget.RecyclerView;

public abstract class BaseRequestListActivity<T> extends BaseRequestActivity {

    protected BaseQuickAdapter<T, BaseViewHolder> mAdapter;

    protected IPaginationPresenter<IPaginationView<T>, T> mPresenter;


    @Override
    protected int getTitleLayoutRes() {

        return R.layout.include_activity_title;
    }

    @Override
    protected int getContentLayoutId() {

        return R.layout.include_smart_refresh_recycler_view;
    }

    @Override
    protected void initialize(Bundle savedInstanceState) {
        ActivityTitleHelper.setTitle(this, getActivityTitle());
        ViewUtils.addStatusBarHeightPadding(findViewById(R.id.layout_title_container));

        RecyclerView recyclerView = findViewById(R.id.recycler_view);
        SmartRefreshLayout refreshLayout = findViewById(R.id.smart_refresh_layout);

        mAdapter = createAdapter(recyclerView);
        recyclerView.setAdapter(mAdapter);

        // network
        mPresenter = createPresenter();
        BasicPaginationView<T> paginationView = new BasicPaginationView<>(recyclerView, refreshLayout, this);
        paginationView.setEmptyLayoutResId(R.layout.view_state_layout_empty); // 空内容
        paginationView.setNotMoreFooterLayoutResId(R.layout.view_list_not_more); // 没有更多

        mPresenter.setModelAndView(paginationView);
        mPresenter.requestRefresh();

        // listener
        refreshLayout.setOnRefreshLoadMoreListener(new RefreshLoadMoreView<>(mPresenter));
    }

    protected abstract String getActivityTitle();

    protected abstract BaseQuickAdapter<T, BaseViewHolder> createAdapter(RecyclerView recyclerView);

    protected abstract IPaginationPresenter<IPaginationView<T>, T> createPresenter();

}
